package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * description
 *
 * @author yuanguangbin
 * @date 2024/6/3 17:25
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {


    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        return PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
    }


    @Resource
    private ServeItemMapper serveItemMapper;

    @Resource
    private RegionMapper regionMapper;

    /**
     * 批量新增区域服务
     *
     * @param serveResDTOList
     */
    @Override
    public void add(List<ServeUpsertReqDTO> serveResDTOList) {
        //0. 遍历服务项参数
        serveResDTOList.stream().forEach(serveResDTO -> {
            //1.校验服务项是否为启用状态，不是启用状态不能新增
            ServeItem serveItem = serveItemMapper.selectById(serveResDTO.getServeItemId());
            if (serveItem == null || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException(serveItem.getName() + "该服务未启用无法添加到区域下使用");
            }
            //2.校验是否重复新增
            Integer count = lambdaQuery()
                    .eq(Serve::getRegionId, serveResDTO.getRegionId())
                    .eq(Serve::getServeItemId, serveResDTO.getServeItemId())
                    .count();
            if (count > 0) {
                throw new ForbiddenOperationException(serveItem.getName() + "服务已存在");
            }
            //3.新增服务
            Serve serve = BeanUtil.toBean(serveResDTO, Serve.class);

            //3.1 查询区域对象
            Region region = regionMapper.selectById(serveResDTO.getRegionId());
            //3.2 区域服务设置城市编码
            serve.setCityCode(region.getCityCode());
            baseMapper.insert(serve);
        });
    }

    /**
     * 修改区域服务价格
     *
     * @param id
     * @param price
     */
    @Override
    public Serve update(Long id, BigDecimal price) {
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();

        if (!update) {
            throw new CommonException("修改服务价格失败");
        }

        return baseMapper.selectById(id);
    }

    /**
     * 区域服务上架
     *
     * @param id
     */
    public Serve onSale(Long id) {
        //1.校验区域服务不存在
        Serve serve = baseMapper.selectById(id);
        if (serve == null) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //2.草稿或下架状态方可上架
        Integer saleStatus = serve.getSaleStatus();
        if (saleStatus != FoundationStatusEnum.INIT.getStatus() && saleStatus != FoundationStatusEnum.DISABLE.getStatus()) {
            throw new ForbiddenOperationException("草稿或下架状态方可上架");
        }
        //3.服务项为启用状态方可上架
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (serveItem == null) {
            throw new ForbiddenOperationException("服务项不存在");
        }
        Integer activeStatus = serveItem.getActiveStatus();
        if (activeStatus != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项为启用状态方可上架");
        }

        //4.更新上架状态
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!update) {
            throw new CommonException("上架服务失败");
        }
        return baseMapper.selectById(id);

    }

    @Override
    public Serve offSale(Long id) {
        //1.校验区域服务不存在
        Serve serve = baseMapper.selectById(id);
        if (serve == null) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //2.草稿或下架状态方可上架
        Integer saleStatus = serve.getSaleStatus();
        if (saleStatus != FoundationStatusEnum.INIT.getStatus() && saleStatus != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("草稿或上架状态方可下架");
        }
        //3.服务项为启用状态方可上架
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (serveItem == null) {
            throw new ForbiddenOperationException("服务项不存在");
        }
        Integer activeStatus = serveItem.getActiveStatus();
        if (activeStatus != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项为启用状态方可上架");
        }

        //4.更新上架状态
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if (!update) {
            throw new CommonException("上架服务失败");
        }
        return baseMapper.selectById(id);
    }

    @Override
    public Serve onHot(Long id) {
        //1.校验区域服务不存在
        Serve serve = baseMapper.selectById(id);
        if (serve == null) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //2.判断是否是热门
        Integer isHot = serve.getIsHot();
        if (isHot != FoundationStatusEnum.INIT.getStatus()) {
            throw new ForbiddenOperationException("不是热门状态才可以设置热门");
        }
        //3.服务项为启用状态方可上架
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (serveItem == null) {
            throw new ForbiddenOperationException("服务项不存在");
        }
        Integer activeStatus = serveItem.getActiveStatus();
        if (activeStatus != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项为启用状态方可上架");
        }

        //4.更新上架状态
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, FoundationStatusEnum.DISABLE.getStatus())
                .update();
        if (!update) {
            throw new CommonException("设置热门服务失败");
        }
        return baseMapper.selectById(id);
    }

    @Override
    public Serve offHot(Long id) {
        //1.校验区域服务不存在
        Serve serve = baseMapper.selectById(id);
        if (serve == null) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //2.判断是否是热门
        Integer isHot = serve.getIsHot();
        if (isHot != FoundationStatusEnum.DISABLE.getStatus()) {
            throw new ForbiddenOperationException("是热门状态才可以取消热门");
        }
        //3.服务项为启用状态方可上架
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (serveItem == null) {
            throw new ForbiddenOperationException("服务项不存在");
        }
        Integer activeStatus = serveItem.getActiveStatus();
        if (activeStatus != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项为启用状态方可上架");
        }

        //4.更新上架状态
        boolean update = lambdaUpdate()
                .eq(Serve::getId, id)
                .set(Serve::getIsHot, FoundationStatusEnum.INIT.getStatus())
                .update();
        if (!update) {
            throw new CommonException("设置热门服务失败");
        }
        return baseMapper.selectById(id);
    }

    @Override
    public Serve serve(Long id) {
        //1.校验区域服务不存在
        Serve serve = baseMapper.selectById(id);
        if (serve == null) {
            throw new ForbiddenOperationException("区域服务不存在");
        }

        //2.服务项为是否存在
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (serveItem == null) {
            throw new ForbiddenOperationException("服务项不存在");
        }


        //3.删除
        int delete = baseMapper.deleteById(id);
        if (delete==0) {
            throw new CommonException("删除失败");
        }
        return baseMapper.selectById(id);
    }



}


